package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ServerResultVO;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public ServerResultVO<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee loginEmployee = employeeService.getOne(queryWrapper);
        if (loginEmployee == null) {
            return ServerResultVO.error("fail to login");
        }
        if (!loginEmployee.getPassword().equals(password)) {
            return ServerResultVO.error("fail to login");
        }
        if (loginEmployee.getStatus() == 0) {
            return ServerResultVO.error("banned account");
        }
        log.info("employee login:{}", loginEmployee.toString());
        request.getSession().setAttribute("employee", loginEmployee.getId());
        return ServerResultVO.success(loginEmployee);
    }

    @PostMapping("/logout")
    public ServerResultVO<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return ServerResultVO.success("successfully logout");
    }

    @PostMapping
    public ServerResultVO<String> saveEmployee(HttpServletRequest request, @RequestBody Employee employee) {

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        log.info("add new employee, info:{}", employee.toString());
        return ServerResultVO.success("successfully add employee");
    }

    @PutMapping
    public ServerResultVO<String> updateById(HttpServletRequest request, @RequestBody Employee employee) {
        employeeService.updateById(employee);
        return ServerResultVO.success("successfully updated");
    }

    @GetMapping("/page")
    public ServerResultVO<Page> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getName);
        return ServerResultVO.success((Page<Employee>)employeeService.page(pageInfo, queryWrapper));
    }

    @GetMapping("/{id}")
    public ServerResultVO<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return ServerResultVO.error("getById fail");
        }
        return ServerResultVO.success(employee);
    }
}
